//
//  RecommendCollectionViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/17.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "RecommendCollectionViewCell.h"
#import "CustomFlowLayout.h"

@interface RecommendCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
/**
 *  源数据
 */
@property (nonatomic, retain) NSMutableArray *itemArr;


@end

@implementation RecommendCollectionViewCell



- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
        [self dataHandle];
    }
    return self;
}

- (void)layoutSubviews {
    _collection.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
}

- (void)createCollection {
    
    CustomFlowLayout *layout = [[CustomFlowLayout alloc] init];
//    CGFloat height = self.bounds.size.height - (self.bounds.size.width - 60) / 2;
    layout.minimumLineSpacing = 60;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake((self.bounds.size.height - (self.bounds.size.width - 60)) / 2, 30, (self.bounds.size.height - (self.bounds.size.width - 60)) / 2, 30);
    
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collection registerClass:[CollectionViewCell13 class] forCellWithReuseIdentifier:@"type13"];
    [_collection registerClass:[CollectionViewCell5 class] forCellWithReuseIdentifier:@"type5"];
    [self.contentView addSubview:_collection];
    _collection.pagingEnabled = YES;
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _itemArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 根据type值来判断到底用那种cell
    LXDLayoutModel *model = _itemArr[indexPath.row];
    if (model.smallPicUrls.count >= 6) {
        CollectionViewCell13 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"type13" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    } else {
        CollectionViewCell5 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"type5" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.bounds.size.width - 60, self.bounds.size.width - 60);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LXDLayoutModel *model = _itemArr[indexPath.row];
    if ([model.type isEqualToString:@"13"] || [model.type isEqualToString:@"21"] || [model.type isEqualToString:@"22"] ) {
        LXDTypeViewController *vc = [[LXDTypeViewController alloc] init];
        vc.ID = model.ID;
        vc.type = model.type;
        if ([model.type isEqualToString:@"13"] || [model.type isEqualToString:@"22"]) {
            vc.pic = model.smallPicUrls[0];
        } else {
            vc.pic = model.picUrl;
        }
        [[self getViewController] pushViewController:vc animated:YES];
    }
    if ([model.type isEqualToString:@"5"]) {
         BYJCarouselViewController *vc = [[BYJCarouselViewController alloc] init];
         vc.value = model.ID;
         [[self getViewController] pushViewController:vc animated:YES];
    }
    if ([model.type isEqualToString:@"20"]) {
        LXDSongListViewController *vc = [[LXDSongListViewController alloc] init];
        LXDSingerModel *songer = [[LXDSingerModel alloc] init];
        songer.singer_id = [NSNumber numberWithInteger:[model.ID integerValue]];
        songer.pic_url = model.picUrl;
        vc.singer = songer;
        [[self getViewController] pushViewController:vc animated:YES];
    }
}

// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
        }
        
    }
    return nil;
}

- (void)dataHandle {
    
    [APPTools GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=1&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=aa72cb7bc75b694367df498bce93ad8cccf94c67&idfa=65D10F72-CCD5-48C8-9DA0-763CD1F86F1B&utdid=VX7HDGZhmzUDADVGvATj2Zei&alf=201200&bundle_id=com.ttpod.music&latitude=38.88255&longtitude=121.5394" par:nil success:^(id responseObject) {
        NSMutableArray *arr = [LXDLayoutModel modelHandleWithArray:responseObject[@"data"]];
        // 排除掉15和18，因为没找到接口，暂时排除
        self.itemArr = [NSMutableArray array];
        for (LXDLayoutModel *model in arr) {
            if ([model.type isEqualToString:@"15"] || [model.type isEqualToString:@"18"]) {
//                NSLog(@"15和18被排除");
            } else {
                [_itemArr addObject:model];
            }
        }
        [_collection reloadData];
    } filed:^(NSError *error) {
        
    }];
    
}










@end
